package com.peas.platform.module.icims.base.mdm.domain.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.mdm.adapter.repository.RegionRepository;
import com.peas.platform.module.icims.base.mdm.adapter.repository.po.RegionPo;
import com.peas.platform.module.icims.base.mdm.domain.RegionDomain;
import com.peas.platform.module.icims.base.mdm.dto.RegionDto;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class RegionDomainImpl extends BaseDomainImpl<RegionDto, RegionPo> implements RegionDomain {

    @Resource
    private RegionRepository regionRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<RegionPo> getMapper() {
        return regionRepository;
    }

    @Override
    public Class<? extends BaseRepository<RegionPo>> getRepositoryClass() {
        return RegionRepository.class;
    }

    @Transactional
    @Override
    public int save(RegionDto dto) {
        if (StrUtil.isBlank(dto.getRegionCode())) {
            String code = sequenceService.genSequence("base", "RegionDto", "ED", 4, 1);
            dto.setRegionCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("regionCode", dto.getRegionCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(RegionDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("regionCode", dto.getRegionCode());
        return result;
    }

}
